package com.seo.service.user.menu.impl;

import java.util.Map;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.seo.mapper.user.menu.UserMenuMapper;
import com.seo.pojo.InputBody;
import com.seo.pojo.ReturnBody;
import com.seo.service.user.menu.sdk.UserMenuService;

@Service("userMenuService")
public class UserMenuServiceImpl implements UserMenuService {

    @Autowired
    private UserMenuMapper userMenuMapper;

    @Override
    public List<Map<String, Object>> getMenuByParentId(int id) {

        return userMenuMapper.getMenuByParentId(id);
    }

    @Override
    public Map<String, Object> getMenuById(int id) {
        return userMenuMapper.getMenuById(id);
    }

    @Override
    public List<Map<String, Object>> getAllMenuList() {
        return userMenuMapper.getAllMenuList();
    }

    @Override
    public ReturnBody saveMenu(InputBody body) {
        ReturnBody rb = ReturnBody.init();
        Map<String, Object> params = body.getParams();

        int num = userMenuMapper.saveMenu(params);
        if (num == 0) {
            return rb.rollback();
        }

        return rb.success();
    }
    
    

    @Override
    public ReturnBody updateMenu(InputBody body) {
        ReturnBody rb = ReturnBody.init();
        Map<String, Object> params = body.getParams();
        int num = userMenuMapper.updateMenu(params);
        if (num == 0) {
            return rb.rollback();
        }
        return rb.success();
    }

    @Override
    public ReturnBody deleteMenu(Integer id) {
        ReturnBody rb = ReturnBody.init();
        List<Map<String, Object>> list = userMenuMapper.getMenuByParentId(id);
        if (list.size() > 0) {
            return rb.error("error", "请删除子菜单");
        }

        int num = userMenuMapper.deleteMenu(id);
        if(num == 0){
            return rb.error("error", "菜单删除失败");
        }
        return rb.success();
    }

}
